from wms import wmsservice as db
from flask import Blueprint, jsonify, redirect, url_for, request
from werkzeug.datastructures import FileStorage
import logging, requests
import sys, traceback, os, re
from utils.tokenutil import TokenUtil
import utils.didiutils as utils 

logger = logging.getLogger('wmsview')

wms = Blueprint("wms", __name__, url_prefix="/api")


# 新增/更新浏览记录
@wms.route('/browse/addOrUpdateBrowse', methods=['POST'])
def addOrUpdateBrowse():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    if formdata['type'] == 1:
        param['type'] = formdata['type']
        param['resumeId'] = formdata['resumeId']
        param['memberId'] = formdata['memberId']
        param['companyId'] = formdata['companyId']
        param['comMemberId'] = TokenUtil.getMemberId(token)
    else:
        param['type'] = 0
        param['memberId'] = TokenUtil.getMemberId(token)
        param['positionId'] = formdata['positionId']
        param['companyId'] = formdata['companyId']
        param['comMemberId'] = formdata['comMemberId']
        param['tcode'] = formdata['tcode']
    return jsonify(db.addOrUpdateBrowse(param))

# 查询浏览记录列表
@wms.route('/browse/getBrowseList', methods=['GET'])
def getBrowseList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['type'] = request.args.get('type')
    return jsonify(db.getBrowseList(param))

# 删除浏览记录
@wms.route('/browse/deleteBrowse', methods=['GET'])
def deleteBrowse():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.deleteBrowse(param))

# 查询收藏列表
@wms.route('/collect/getCollectList', methods=['GET'])
def getCollectList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['type'] = request.args.get('type')
    return jsonify(db.getCollectList(param))

# 查询沟通记录列表
@wms.route('/comm/getCommList', methods=['GET'])
def getCommList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['type'] = request.args.get('type')
    return jsonify(db.getCommList(param))

# 查询沟通记录详情
@wms.route('/comm/getCommDetail', methods=['GET'])
def getCommDetail():
    param = {}
    param['memberId'] = request.args.get('memberId')
    param['comMemberId'] = request.args.get('comMemberId')
    param['companyId'] = request.args.get('companyId')
    return jsonify(db.getCommDetail(param))

# 新增/更新沟通记录
@wms.route('/comm/addOrUpdateComm', methods=['POST'])
def addOrUpdateComm():
    formdata = request.json
    param = {}
    param['positionId'] = formdata['positionId']
    param['memberId'] = formdata['memberId']
    param['companyId'] = formdata['companyId']
    param['comMemberId'] = formdata['comMemberId']
    return jsonify(db.addOrUpdateComm(param))

# 查询看过我列表
@wms.route('/browse/getVisitList', methods=['GET'])
def getVisitList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['type'] = request.args.get('type')
    if param['type'] == '1':
        param['comMemberId'] = TokenUtil.getMemberId(token)
    else:
        param['memberId'] = TokenUtil.getMemberId(token)
    return jsonify(db.getVisitList(param))

# 新增/更新收藏信息
@wms.route('/collect/addOrUpdateCollect', methods=['POST'])
def addOrUpdateCollect():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    if formdata['type'] == 1:
        param['type'] = formdata['type']
        param['resumeId'] = formdata['resumeId']
        param['memberId'] = formdata['memberId']
        param['comMemberId'] = TokenUtil.getMemberId(token)
    else:
        param['memberId'] = TokenUtil.getMemberId(token)
        param['type'] = formdata['type']
        param['positionId'] = formdata['positionId']
        param['companyId'] = formdata['companyId']
        param['comMemberId'] = formdata['comMemberId']
    return jsonify(db.addOrUpdateCollect(param))

# 删除收藏
@wms.route('/collect/deleteCollect', methods=['POST'])
def deleteCollect():
    formdata = request.json
    param = {}
    param['id'] = formdata['id']
    return jsonify(db.deleteCollect(param))

# 新增/更新职位申请
@wms.route('/apply/addOrUpdateApply', methods=['POST'])
def addOrUpdateApply():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['positionId'] = formdata['positionId']
    param['companyId'] = formdata['companyId']
    param['comMemberId'] = formdata['comMemberId']
    param['tcode'] = formdata['tcode']
    return jsonify(db.addOrUpdateApply(param))

# 新增/更新分享记录
@wms.route('/share/addOrUpdateShare', methods=['POST'])
def addOrUpdateShare():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['type'] = formdata['type']
    param['dataId'] = formdata['dataId']
    param['companyId'] = formdata['companyId']
    param['comMemberId'] = formdata['comMemberId']
    return jsonify(db.addOrUpdateShare(param))

# 查询简历信息
@wms.route('/wms/getResumeList', methods=['GET'])
def wmsgetResumeList():
    param = {}
    if request.args.get('id') is not None:
        if request.args.get('id') != '':
            param['id'] = request.args.get('id')
    if request.args.get('keyword') is not None:
        if request.args.get('keyword') != '':
            param['keyword'] = request.args.get('keyword')
    if request.args.get('postId') is not None:
        if request.args.get('postId') != '':
            param['postId'] = request.args.get('postId')
    if request.args.get('education') is not None:
        if request.args.get('education') != '':
            param['education'] = request.args.get('education')
    if request.args.get('salary') is not None:
        if request.args.get('salary') != '':
            param['salary'] = request.args.get('salary')
    return jsonify(db.wmsgetResumeList(param))

# 查询简历完整信息
@wms.route('/wms/getResume', methods=['GET'])
def wmsgetResume():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['id'] = request.args.get('id')
    return jsonify(db.wmsgetResume(param))

# 查询简历基本信息
@wms.route('/wms/getResumeInfo', methods=['GET'])
def getResumeInfo():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    return jsonify(db.getResumeInfo(param))

# 更新简历基本信息
@wms.route('/wms/updateResumeBasic', methods=['POST'])
def updateResumeBasic():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['id'] = formdata['id']
    if 'jobStatus' in formdata.keys():
        param['jobStatus'] = formdata['jobStatus']
    if 'personalSkill' in formdata.keys():
        param['personalSkill'] = formdata['personalSkill']           
    return jsonify(db.updateResumeBasic(param))

# 更新简历基本信息（详细）
@wms.route('/wms/updateResumeBasicInfo', methods=['POST'])
def updateResumeBasicInfo():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['avatar'] = formdata['avatar']
    param['birthday'] = formdata['birthday']
    param['timeToWork'] = formdata['timeToWork']
    param['name'] = formdata['name']
    param['sex'] = formdata['sex']
    param['education'] = formdata['education']
    param['phone'] = formdata['phone']
    param['email'] = formdata['email']
    param['status'] = formdata['status']
    return jsonify(db.updateResumeBasicInfo(param))

# 查询职位申请列表
@wms.route('/apply/getApplyList', methods=['GET'])
def getApplyList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['type'] = request.args.get('type')
    logger.info("==== memberId：")
    logger.info(param['memberId'])
    logger.info("==== type：")
    logger.info(param['type'])
    return jsonify(db.getApplyList(param))

# 添加职位申请查看记录
@wms.route('/apply/addApplyRead', methods=['POST'])
def addApplyRead():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['applyId'] = formdata['applyId']
    return jsonify(db.addApplyRead(param))

# 删除职位申请
@wms.route('/apply/deleteApply', methods=['POST'])
def deleteApply():
    formdata = request.json
    param = {}
    param['id'] = formdata['id']
    return jsonify(db.deleteApply(param))

# 更新简历状态
@wms.route('/apply/updateApplyStatus', methods=['POST'])
def updateApplyStatus():
    formdata = request.json
    param = {}
    param['id'] = formdata['id']
    param['status'] = formdata['status']
    return jsonify(db.updateApplyStatus(param))

# 查询期望职位详情
@wms.route('/wms/getJobExpectDetail', methods=['GET'])
def getJobExpectDetail():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.getJobExpectDetail(param))

# 新增/更新期望职位
@wms.route('/wms/updateJobExpect', methods=['POST'])
def updateJobExpect():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['id'] = formdata['id']
    param['jobType'] = formdata['jobType']
    param['postId'] = formdata['postId']
    param['postName'] = formdata['postName']
    param['industryId'] = formdata['industryId']
    param['industryName'] = formdata['industryName']
    param['expectSalary'] = formdata['expectSalary']
    return jsonify(db.updateJobExpect(param))

# 删除期望职位
@wms.route('/wms/deleteJobExpectById', methods=['GET'])
def deleteJobExpectById():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.deleteJobExpectById(param))

# 查询工作经历详情
@wms.route('/wms/getWorkExpDetail', methods=['GET'])
def getWorkExpDetail():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.getWorkExpDetail(param))

# 新增/更新工作经历
@wms.route('/wms/updateWorkExp', methods=['POST'])
def updateWorkExp():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['id'] = formdata['id']
    param['postId'] = formdata['postId']
    param['postName'] = formdata['postName']
    param['industryId'] = formdata['industryId']
    param['industryName'] = formdata['industryName']
    param['startDate'] = formdata['startDate']
    param['endDate'] = formdata['endDate']    
    param['jobContent'] = formdata['jobContent']
    param['companyName'] = formdata['companyName']
    param['department'] = formdata['department']
    param['skill'] = formdata['skill']  
    return jsonify(db.updateWorkExp(param))

# 删除工作经历
@wms.route('/wms/deleteWorkExpById', methods=['GET'])
def deleteWorkExpById():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.deleteWorkExpById(param))

# 查询项目经历详情
@wms.route('/wms/getProExpDetail', methods=['GET'])
def getProExpDetail():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.getProExpDetail(param))

# 新增/更新项目经历
@wms.route('/wms/updateProExp', methods=['POST'])
def updateProExp():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    logger.info(formdata)
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['id'] = formdata['id']
    param['startDate'] = formdata['startDate']
    param['endDate'] = formdata['endDate']
    param['name'] = formdata['name']
    param['role'] = formdata['role']
    param['startDate'] = formdata['startDate']
    param['endDate'] = formdata['endDate']    
    param['descr'] = formdata['descr']
    param['performance'] = formdata['performance']
    param['url'] = formdata['url']  
    return jsonify(db.updateProExp(param))

# 删除项目经历
@wms.route('/wms/deleteProExpById', methods=['GET'])
def deleteProExpById():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.deleteProExpById(param))

# 查询教育经历详情
@wms.route('/wms/getEduExpDetail', methods=['GET'])
def getEduExpDetail():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.getEduExpDetail(param))

# 新增/更新教育经历
@wms.route('/wms/updateEduExp', methods=['POST'])
def updateEduExp():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    logger.info(formdata)
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['id'] = formdata['id']
    param['school'] = formdata['school']
    param['major'] = formdata['major']
    param['education'] = formdata['education']
    param['startTime'] = formdata['startTime']
    param['endTime'] = formdata['endTime']
    param['schoolExp'] = formdata['schoolExp']    
    return jsonify(db.updateEduExp(param))

# 删除教育经历
@wms.route('/wms/deleteEduExpById', methods=['GET'])
def deleteEduExpById():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.deleteEduExpById(param))

# 新增/更新屏蔽公司
@wms.route('/shield/addOrUpdateShield', methods=['POST'])
def addOrUpdateShield():
    formdata = request.json
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['type'] = formdata['type']
    param['companyId'] = formdata['companyId']
    return jsonify(db.addOrUpdateShield(param))

# 查询屏蔽公司列表
@wms.route('/shield/getShieldList', methods=['GET'])
def getShieldList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    return jsonify(db.getShieldList(param))

# 删除屏蔽公司
@wms.route('/shield/deleteShield', methods=['GET'])
def deleteShield():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.deleteShield(param))

# 查询附件简历列表
@wms.route('/wms/getResumeFileList', methods=['GET'])
def getResumeFileList():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    return jsonify(db.getResumeFileList(param))

# 删除附件简历
@wms.route('/wms/deleteResumeFileById', methods=['GET'])
def deleteResumeFileById():
    param = {}
    param['id'] = request.args.get('id')
    return jsonify(db.deleteResumeFileById(param))

# 简历附件上传
@wms.route('/file/uploadResumeFile', methods=['POST'])
def uploadResumeFile():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    file = request.files.get('file')
    filenm = request.form.get('filenm')
    url = request.base_url
    url = url[:re.search('api', url).start()] + "resume/"
    param = {}
    param['memberId'] = TokenUtil.getMemberId(token)
    param['fileExt'] = filenm.split('.')[1]
    param['fileSize'] = utils.getfilesize(int(request.form.get('filesize')))
    param['fileUrl'] = url
    filenm = db.getfilename(param)
    
    dir_path = str(os.path.abspath(os.path.dirname(__file__)))
    dirpath = os.path.join(dir_path.strip('wms'), 'resume')
    file.save(dirpath + '/' + filenm)
    return jsonify('ok')

# 上传图片
@wms.route('/uploadimg', methods=['POST'])
def uploadimg():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    file = request.files.get('files')
    url = request.base_url
    url = url[:re.search('api', url).start()] + "useravatar/"
    filename = str(TokenUtil.getMemberId(token)) + '.' + file.filename.split('.')[1]
    dir_path = str(os.path.abspath(os.path.dirname(__file__)))
    dirpath = os.path.join(dir_path.strip('wms'), 'useravatar')
    file.save(dirpath + '/' + filename)

    return url + filename

# 上传图片logo
@wms.route('/uploadlogo', methods=['POST'])
def uploadlogo():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    file = request.files.get('files')
    filename = ''
    img_count = request.form.get('img_count')
    if img_count is not None:
        companyId = db.getCompanyId(TokenUtil.getMemberId(token))
        filename = str(companyId) + '_' + str(img_count) + '.' + file.filename.split('.')[1]
    else:
        filename = str(TokenUtil.getMemberId(token)) + '.' + file.filename.split('.')[1]
    url = request.base_url
    url = url[:re.search('api', url).start()] + "companylogo/"
    dir_path = str(os.path.abspath(os.path.dirname(__file__)))
    dirpath = os.path.join(dir_path.strip('wms'), 'companylogo')
    file.save(dirpath + '/' + filename)
    logger.info(url + filename)
    return url + filename

# 上传图片license
@wms.route('/uploadlicense', methods=['POST'])
def uploadlicense():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    file = request.files.get('files')
    url = request.base_url
    url = url[:re.search('api', url).start()] + "companylicense/"
    companyId = db.getCompanyId(TokenUtil.getMemberId(token))
    filename = str(companyId) + '.' + file.filename.split('.')[1]
    dir_path = str(os.path.abspath(os.path.dirname(__file__)))
    dirpath = os.path.join(dir_path.strip('wms'), 'companylicense')
    file.save(dirpath + '/' + filename)

    return url + filename

# 导出简历
@wms.route('/wms/exportWord', methods=['GET'])
def exportWord():
    # 从HTTP头部中获取token
    token = request.headers.get('token')
    url = request.base_url
    url = url[:re.search('api', url).start()] + "resume/"
    param = {}
    param['id'] = request.args.get('id')
    param['memberId'] = TokenUtil.getMemberId(token)
    param['fileUrl'] = url
    param['fileExt'] = 'pdf'
    return jsonify(db.exportWord(param))
